import {_decorator, Component, Label, Node, UITransform, Vec3} from "cc";
import {configsHelper} from "db://assets/script/configs-helper";
import {lobby} from "db://assets/script/lobby";
import Long from "long";
import {fishConfigsLoader} from "db://assets/script/configs/fish-configs-loader";
import {cmm} from "db://assets/script/utils/cmm";
import Tuple2 = cmm.Tuple2;

const {ccclass, property} = _decorator;

@ccclass('PropDesc')
export class PropDesc extends Component {

    @property(Label)
    propName!: Label;

    @property(Label)
    propDesc!: Label;

    start() {
        this.propDesc.overflow = Label.Overflow.RESIZE_HEIGHT; // 高度自适应
        this.propDesc.horizontalAlign = Label.HorizontalAlign.LEFT; // 左对齐
        this.propDesc.verticalAlign = Label.VerticalAlign.TOP; // 顶部对齐
        this.propDesc.getComponent(UITransform)!.width = 180; // 设置最大宽度（超出则换行）
    }

    init(targetNode: Node, assetId: number) {
        const config = configsHelper.loadAssetConfig(assetId)
        if (config) {
            this.propName.string = config.name
            this.propDesc.string = config.desc
            this.node.worldPosition = new Vec3(targetNode.worldPositionX - 32.5, targetNode.worldPositionY - 65 * 1.5 - 3)
            lobby.instance.showClickMask(0, () => this.node.destroy())
        }
    }

    initPeculiarity(targetNode: Node, peculiarityId: Long) {
        const pc = configsHelper.loadPeculiarityConfig(peculiarityId)!
        if (pc) {
            this.propName.string = pc.name
            this.propDesc.string = pc.desc
            this.node.worldPosition = new Vec3(targetNode.worldPositionX - 32.5, targetNode.worldPositionY - 32 * 1.5 - 3)
            lobby.instance.showClickMask(0, () => this.node.destroy())
        }
    }

    initLingGen(targetNode: Node, lingGen: Map<Long, Long>) {
        this.propName.string = '灵根详情'
        let tmp = cmm.map2array(lingGen, (k, v) =>
            new Tuple2(k, new Tuple2(fishConfigsLoader.findLingGenConfig(k)!, v))
        );
        tmp = tmp.sort((o1, o2) => o1.t2.t2.compare(o2.t2.t2))
        let desc = ``
        for (let ele of tmp) {
            const name = ele.t2.t1.name
            const rate = (ele.t2.t2.toNumber() / 100).toFixed(0)
            desc += `${name} 占比 ${rate}%\n`
        }
        for (let ele of tmp) {
            desc += `${ele.t2.t1.name}: ${ele.t2.t1.desc}\n`
        }
        this.propDesc.string = desc
        this.node.worldPosition = new Vec3(targetNode.worldPositionX - 32.5, targetNode.worldPositionY - 32 * 1.5 - 3)
        lobby.instance.showClickMask(0, () => this.node.destroy())
    }


    initGrowthPoint(targetNode: Node, growthPoint: Map<number, number>) {
        this.propName.string = '属性详情'
        this.propDesc.string = `
            ${configsHelper.loadAssetConfig(600001)!.name}：${growthPoint.get(600001)}
            ${configsHelper.loadAssetConfig(600002)!.name}：${growthPoint.get(600002)}
            ${configsHelper.loadAssetConfig(600003)!.name}：${growthPoint.get(600003)}
            ${configsHelper.loadAssetConfig(600004)!.name}：${growthPoint.get(600004)}
            ${configsHelper.loadAssetConfig(600005)!.name}：${growthPoint.get(600005)}
            `
        this.node.worldPosition = new Vec3(targetNode.worldPositionX - 32.5, targetNode.worldPositionY - 32 * 1.5 - 3)
        lobby.instance.showClickMask(0, () => this.node.destroy())
    }


}